﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AG.Editor.Core;
using AG.Editor.Core.Data;
using AG.Editor.UI;
using AG.Editor.ModelUI.Windows;
using AG.Editor.ModelUI.Controls;

namespace AG.Editor.ModelUI
{
    public partial class AGEModelMgrPanel : UserControl, IAGEMainComponent
    {
        private AGEPreviewModelPanel _previewPanel;

        public AGEModelMgrPanel()
        {
            InitializeComponent();

            BindModelTree();

            _previewPanel = new AGEPreviewModelPanel();
            _previewPanel.Dock = DockStyle.Fill;
            this.splitContainer1.Panel2.Controls.Add(_previewPanel);
        }

        private void BindModelTree()
        {
            ctlTreeModels.Nodes.Clear();
            foreach (var modelCategory in AGEContext.Current.EProject.TProject.ModelCategories)
            {
                TreeNode tnCategory = new TreeNode();
                tnCategory.Text = modelCategory.Caption;
                tnCategory.Tag = modelCategory;
                this.ctlTreeModels.Nodes.Add(tnCategory);

                List<AGModelRef> models = AGEContext.Current.EProject.Models.Where(p => p.CategoryId == modelCategory.Id).ToList();
                for (int iModel = 0; iModel < models.Count; iModel++)
                {
                    AGModelRef model = models[iModel];
                    TreeNode tnModel = new TreeNode();
                    tnModel.Text = model.ToString();
                    tnModel.Tag = model;
                    tnCategory.Nodes.Add(tnModel);
                }
            }
            ctlTreeModels.ExpandAll();
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            if (this.Visible)
            {
            }

            base.OnVisibleChanged(e);
        }

        /// <summary>
        /// 删除模型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void miRemoveModel_Click(object sender, EventArgs e)
        {
            TreeNode selNode = ctlTreeModels.SelectedNode;
            if (selNode == null)
            {
                return;
            }

            AGModelRef modelRef = selNode.Tag as AGModelRef;
            if (modelRef == null)
            {
                return;
            }

            if (MessageBox.Show(string.Format("是否要删除模型[{0}]", modelRef.Caption), "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                AGEContext.Current.EProject.RemoveModel(modelRef.ModelUniqueId);
                BindModelTree();
            }
        }

        /// <summary>
        /// 创建新模型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void miCreateModel_Click(object sender, EventArgs e)
        {
            AGEEditModelWindow window = new AGEEditModelWindow();
            window.ShowDialog();
            if (window.SavedModel != null)
            {
                AGEContext.Current.EProject.AddModel(window.SavedModel);
                BindModelTree();
            }
        }

        /// <summary>
        /// 导入模型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void miImportModel_Click(object sender, EventArgs e)
        {
            AGImportModelWindow window = new AGImportModelWindow();
            if (window.ShowDialog() == DialogResult.OK)
            {
                AGEContext.Current.EProject.AddModel(window.ImportedModel);
                BindModelTree();
            }
        }

        void miEditModel_Click(object sender, EventArgs e)
        {
            TreeNode selNode = ctlTreeModels.SelectedNode;
            if (selNode == null)
            {
                return;
            }

            AGModelRef modelRef = selNode.Tag as AGModelRef;
            if (modelRef == null)
            {
                return;
            }

            AGModel model = AGTools.Current.ModelStore.GetModel(AGEContext.Current.EProject, modelRef);

            AGEEditModelWindow window = new AGEEditModelWindow(model);
            window.ShowDialog();
        }

        void miEditSummaryModel_Click(object sender, EventArgs e)
        {
            TreeNode selNode = ctlTreeModels.SelectedNode;
            if (selNode == null)
            {
                return;
            }

            AGModelRef modelRef = selNode.Tag as AGModelRef;
            if (modelRef == null)
            {
                return;
            }

            AGModel model = AGTools.Current.ModelStore.GetModel(AGEContext.Current.EProject, modelRef);

            AGEditModelSummaryWindow window = new AGEditModelSummaryWindow(model);
            if (window.ShowDialog() == DialogResult.OK)
            {
                modelRef.CategoryId = model.CategoryId;
                modelRef.ModelId = model.Id;
                modelRef.Caption = model.Caption;
                AGEContext.Current.EProject.Changed();
                AGTools.Current.ModelStore.SaveModel(AGEContext.Current.EProject, model);

                BindModelTree();
            }
        }

        private void ctlTreeModels_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            TreeNode selNode = e.Node;
            AGModelRef selModel = selNode.Tag as AGModelRef;

            if (selModel != null)
            {
                AGModel model = AGTools.Current.ModelStore.GetModel(AGEContext.Current.EProject, selModel);
                _previewPanel.SetModel(model);
            }
        }

        public void OnActived(AGEMainMenuMidiator mmm)
        {
            mmm.Clear();

            ToolStripMenuItem m2 = mmm.AddMenu(new ToolStripMenuItem("发布"));
            ToolStripMenuItem miPublishModel = new ToolStripMenuItem("发布模型");
            miPublishModel.Click += new EventHandler(miPublishModel_Click);
            m2.DropDownItems.Add(miPublishModel);

            ToolStripMenuItem m1 = mmm.AddMenu(new ToolStripMenuItem("模型管理"));
            ToolStripMenuItem miCreateModel = new ToolStripMenuItem("创建模型");
            miCreateModel.Click += new EventHandler(miCreateModel_Click);
            m1.DropDownItems.Add(miCreateModel);
            ToolStripMenuItem miImportModel = new ToolStripMenuItem("导入模型");
            miImportModel.Click += new EventHandler(miImportModel_Click);
            m1.DropDownItems.Add(miImportModel);

            ToolStripMenuItem miEditModel = new ToolStripMenuItem("修改模型");
            miEditModel.Click += new EventHandler(miEditModel_Click);
            m1.DropDownItems.Add(miEditModel);

            ToolStripMenuItem miRemoveModel = new ToolStripMenuItem("删除模型");
            miRemoveModel.Click += new EventHandler(miRemoveModel_Click);
            m1.DropDownItems.Add(miRemoveModel);

            ToolStripMenuItem miEditSummaryModel = new ToolStripMenuItem("修改模型基本信息");
            miEditSummaryModel.Click += new EventHandler(miEditSummaryModel_Click);
            m1.DropDownItems.Add(miEditSummaryModel);
        }

        /// <summary>
        /// 发布模型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void miPublishModel_Click(object sender, EventArgs e)
        {
            TreeNode selNode = ctlTreeModels.SelectedNode;
            if (selNode == null)
            {
                return;
            }

            AGModelRef selModel = selNode.Tag as AGModelRef;

            if (selModel != null)
            {
                AGModel model = AGTools.Current.ModelStore.GetModel(AGEContext.Current.EProject, selModel);
                if (!model.Validate())
                {
                    MessageBox.Show("发布失败，模型验证不通过!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (MessageBox.Show(string.Format("是否要发布模型[{0}]?", selModel.Caption),"提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    FolderBrowserDialog dlg = new FolderBrowserDialog();
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        AGTools.Current.ModelPublisher.Publish(AGEContext.Current.EProject, model, dlg.SelectedPath);
                        MessageBox.Show("发布成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

    }
}
